/*
 * Created by JFormDesigner on Sun Jun 30 11:21:16 HKT 2024
 */

package com.hongyan.view.student;

import com.hongyan.base.BaseContentJpanel;
import com.hongyan.bean.Student;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.UserDao;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;

/**
 * @author hongyan
 */
public class SwingStudentInfoPanel extends BaseContentJpanel {
    private static String[] sex = {"男", "女"};
    private static ArrayList<String> college = new ArrayList<>(Arrays.asList((("建筑学院\n" + "信息科学技术学院\n" + "五道口金融学院\n" + "新闻与传播学院\n" + "法学院\n" + "社会科学学院\n" + "航天航空学院\n" + "机械工程学院\n" + "人文学院\n" + "马克思主义学院\n" + "环境学院\n" + "公共管理学院").split("\n"))));

    public SwingStudentInfoPanel() {
        super();
        initData();
    }

    @Override
    public void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        textField3 = new JTextField();
        label5 = new JLabel();
        comboBox1 = new JComboBox(sex);
        comboBox2 = new JComboBox(college.toArray());
        button1 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("个人信息修改");
        label1.setFont(new Font("黑体", Font.PLAIN, 18));
        add(label1);
        label1.setBounds(new Rectangle(new Point(285, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("姓名：");
        add(label2);
        label2.setBounds(new Rectangle(new Point(195, 135), label2.getPreferredSize()));
        add(textField1);
        textField1.setBounds(305, 130, 200, textField1.getPreferredSize().height);

        //---- label3 ----
        label3.setText("性别：");
        add(label3);
        label3.setBounds(195, 175, 39, 17);

        //---- label4 ----
        label4.setText("联系方式：");
        add(label4);
        label4.setBounds(195, 215, 85, 17);
        add(textField3);
        textField3.setBounds(305, 210, 200, textField3.getPreferredSize().height);

        //---- label5 ----
        label5.setText("学院：");
        add(label5);
        label5.setBounds(195, 255, 39, 17);
        add(comboBox1);
        comboBox1.setBounds(305, 170, 200, comboBox1.getPreferredSize().height);
        add(comboBox2);
        comboBox2.setBounds(305, 255, 200, comboBox2.getPreferredSize().height);

        //---- button1 ----
        button1.setText("修改");
        button1.setFont(new Font("黑体", Font.PLAIN, 20));
        add(button1);
        button1.setBounds(260, 325, 178, 70);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    @Override
    public void initData() {
        Student studentById = UserDao.findStudentById(MainFrameConfig.getId());
        textField1.setText(studentById.getS_name());
        if ("男".equals(studentById.getSex())) {
            comboBox1.setSelectedIndex(0);
        } else {
            comboBox1.setSelectedIndex(1);
        }
        textField3.setText(studentById.getContact());
    }

    @Override
    public void initListen() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Student student = new Student();
                student.setS_id(MainFrameConfig.getId());
                student.setS_name(textField1.getText());
                student.setSex(sex[comboBox1.getSelectedIndex()]);
                student.setContact(textField3.getText());
                student.setCollege(college.get(comboBox2.getSelectedIndex()));
                if (UserDao.updateStudent(student) > 0) {
                    showToast("修改成功");
                    initData();
                } else {
                    showToast("修改失败");
                }

            }
        });
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField3;
    private JLabel label5;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
